<template>
  <div class="warehouse-management">
    <el-row :gutter="20" class="mb-20">
      <el-col :span="24">
        <el-button type="primary" @click="showAddDialog">添加仓库</el-button>
      </el-col>
    </el-row>

    <el-table
        :data="tableData"
        border
        style="width: 100%"
        v-loading="loading">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="name" label="仓库名称" width="150" />
      <el-table-column prop="portId" label="所属港口" width="100" />
      <el-table-column prop="maxCapacity" label="最大容量（默认为100吨）" width="100" />
      <el-table-column prop="currentCapacity" label="当前存储量" width="100" />
      <el-table-column prop="stock" label="件数" width="100" />
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="50%">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="120px">
        <el-form-item label="仓库名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="所属港口" prop="portId">
          <el-input-number v-model="form.portId" :min="1" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getWarehouseList, createWarehouse, updateWarehouse, deleteWarehouse } from '@/api/setting'

const tableData = ref([])
const loading = ref(false)
const dialogVisible = ref(false)
const dialogTitle = ref('添加仓库')
const formRef = ref(null)

const form = ref({
  id: null,
  name: '',
  maxCapacity: null,
  currentCapacity: null,
  portId: null,
  stock:null
})

const rules = {
  name: [{ required: true, message: '请输入仓库名称', trigger: 'blur' }],
  portId: [{ required: true, message: '请输入所属港口', trigger: 'blur' }],
}

const fetchData = async () => {
  loading.value = true
  try {
    const response = await getWarehouseList()
    tableData.value = response.data
  } catch (error) {
    ElMessage.error('获取数据失败')
  } finally {
    loading.value = false
  }
}

const showAddDialog = () => {
  dialogTitle.value = '添加仓库'
  form.value = {
    id: null,
    name: '',
    maxCapacity: 0,
    currentCapacity: 0,
    portId: null,
    stock:null,
  }
  dialogVisible.value = true
}

const handleEdit = (row) => {
  dialogTitle.value = '编辑仓库'
  form.value = { ...row }
  dialogVisible.value = true
}

const handleDelete = (id) => {
  ElMessageBox.confirm('确认删除该仓库?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await deleteWarehouse(id)
    ElMessage.success('删除成功')
    fetchData()
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

const submitForm = async () => {
  try {
    await formRef.value.validate()

    if (form.value.id) {
      await updateWarehouse(form.value)
      ElMessage.success('更新成功')
    } else {
      await createWarehouse(form.value)
      ElMessage.success('添加成功')
    }

    dialogVisible.value = false
    fetchData()
  } catch (error) {
    console.error('表单验证失败', error)
  }
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.mb-20 {
  margin-bottom: 20px;
}
</style> 